package com.zer0.tga.entity.livingentity.mob;

import com.zer0.tga.art.ArtArchive;
import com.zer0.tga.art.Bitmap;
import com.zer0.tga.entity.Entity;
import com.zer0.tga.entity.livingentity.Teemo;
import com.zer0.tga.entity.livingentity.flyingentity.TeemoDart;
import com.zer0.tga.math.Coordinate;
import com.zer0.tga.math.Direction;

public class Wolf extends Mob{

	private boolean goingInX;
	
	private boolean decisionTime = true;
	
	private int shouldTurn = 1;
	
	private int modifier = 0;
	
	
	private Coordinate playerPos = null;
	private Coordinate playerRad = null;
	
	public void init()
	{
		//System.out.println("init");
		unitType = UnitType.ENEMY;
		health = maxHealth = 20;
		movementSpeed = 2.0;
		goingInX = true;
	}

	@Override
	public void action() 
	{
		if(decisionTime)
		{
			for(Entity e:level.getCurrentRoom().entities)
			{
				if (e instanceof Teemo)
				{
					//playerPos = new Coordinate(e.position.x,e.position.y);
					//playerRad = new Coordinate(e.radius.x,e.radius.y);
					playerPos = e.position;
					playerRad = e.radius;
					break;
				}		    	
			}
			if(goingInX) modifier = playerPos.x > position.x ? 1 : -1 ;
			else         modifier = playerPos.y > position.y ? 1 : -1 ;
			
			decisionTime = false;
			movementSpeed = 2.0;
		}
		//System.out.println("Trying to move in " + modifier + (goingInX?"X":"Y") + " with pos " + position.x +" " + position.y +" and player in " + playerPos.x + " " + playerPos.y);
		
		movementSpeed += 0.05;
		
		if(goingInX)
		{
			boolean hit = !move(modifier*movementSpeed,0);
			
			if((shouldTurn==1 && hit) || movementSpeed > 7.0)
				shouldTurn = 2;
			
			direction = modifier > 0 ? Direction.EAST : Direction.WEST;
			
			if((shouldTurn==2 && hit) || (modifier<0 && position.x <= playerPos.x-playerRad.x/2) || (modifier>0 && position.x >= playerPos.x + playerRad.x/2))
			{
				goingInX = false;
				decisionTime = true;
				modifier = modifier > 0 ? -1 : 1;
				shouldTurn = 1;
			}
	
		}
		else
		{
			boolean hit = !move(0,modifier*movementSpeed);
			
			if((shouldTurn==1 && hit)|| movementSpeed > 7.0)
				shouldTurn = 2;

			direction = modifier > 0 ? Direction.SOUTH : Direction.NORTH;
			
			if((shouldTurn==2 && hit) || (modifier<0 && position.y <= playerPos.y-playerRad.y/2) || (modifier>0 && position.y >= playerPos.y+playerRad.y/2))
			{
				goingInX = true;
				decisionTime = true;
				modifier = modifier > 0 ? -1 : 1;
				shouldTurn = 1;
			}

		}
		
	}

	@Override
	public Bitmap getArt() {
		// TODO Auto-generated method stub
		return ArtArchive.badguy2[0][direction.getDirection()-1];
	}
	

	public boolean collision(Entity entity, Direction dir) 
    {
	//	position.x += movementSpeed*knockback_x;
	//	position.y += movementSpeed*knockback_y;
	//	shouldTurn = true;
	//	System.out.println("asd " + entity.getClass().getSimpleName());
		
	//	super.collision(entity,dir);
		if(entity instanceof TeemoDart)
		{
			shouldTurn = 0;
			hurt(((TeemoDart) entity).damage);
			entity.removed = true;
			return false;
		}
		else if (entity instanceof Teemo)
		{
			shouldTurn = 2;
			((Teemo) entity).hurt(damage);
			return false;
		}
		else if ( entity == null)
		{
			return false;
		}
		return true;

    }



}
